<?php
/**
 * Description of anonces
 *
 * @author dmitryy
 */
class Models_Anonces extends Access_Db {
    function setAnonce($arr)
    {
        $query = "INSERT INTO " . $this->lang . "_anonces (title, body, visible, comments, news, user_id) VALUES (?,?,?,?,?,?)";
        $this->prepare($query);
        $this->insert($arr);
        $anonceId = $this->getLAstInsertedID();
        $query = "UPDATE " . $this->lang . "_anonces SET created=NOW(), modified=NOW() where id='$anonceId'";
        $this->execQuery($query);
        $query = "INSERT INTO role_permissions (role_id, article_id, action, controller) VALUES (?,?,?,?)";
        $roleId = $this->getSpecialUserGroup($_COOKIE['user_id']);
        $permissionArr = array(
            '1',                          //!!!!! 1 - is id of GUEST group, need for showing anonce to guests
            $anonceId,
            'anonce',
            'articles'
        );
        $permissionArr1 = array(
            $roleId['role_id'],                        
            $anonceId,
            'editanonce',
            'articles'
        );
        $permissionArr2 = array(
            $roleId['role_id'],                        
            $anonceId,
            'editinganonce',
            'articles'
        );
        $this->prepare($query);
        $this->insert($permissionArr);
        $this->insert($permissionArr1);
        $this->insert($permissionArr2);
    }
    
    function validateAnonce($arr)
    {
      $errors = array();
      if((strlen($arr['0']) > 50) or (strlen($arr['0']) < 1))
      {
        $errors[] = 'Name must be at least 1 and up to 50 characters.';
      }
      if((strlen($arr['1']) < 100))
      {
        $errors[] = 'Content is empty.';
      }
      if(count($errors) > 0)
      {
          return $errors;
      }
      return FALSE;
    }

    function getAnonce($id)
    {
        $select = array(
            'id',
            'title',
            'body',
            'modified',
            'comments',
            'user_id',
        );
        $table = $this->lang . '_anonces';
        $this->select($select, $table, "id='$id' and visible=1");
        $result = $this->getSelected();
        return $result;
    }
    
    function editAnonce($id, $arr)
    {
        $query = "UPDATE " . $this->lang . "_anonces SET title='"
              . $arr['0'] . "', body='"
              . $arr['1'] . "', visible='"
              . $arr['2'] . "', comments='"
              . $arr['3'] . "', news='"
              . $arr['4'] . "', modified=NOW() WHERE id='" . $id . "'";
       $this->execQuery($query);
    }
    
    function getStringOfAnonces($count)
    {
        $select = array(
            'id',
            'title',
            'body',
            'modified'
        );
        $table = $this->lang . '_anonces';
        $where = "visible='1' and news='1' order by modified DESC LIMIT 0,$count";
        $this->select($select, $table, $where);
        $result = array();
        while($row = $this->getSelected())
        {
            $result[] = $row;
        }
        return $result;
    }
    
    function getAllAnonces()
    {
         $select = array(
            'id',
            'title',
            'modified'
        );
        $table = $this->lang . '_anonces';
        $where = "visible='1' and news='1' order by modified DESC";
        $this->select($select, $table, $where);
        $result = array();
        while($row = $this->getSelected())
        {
            $result[] = $row;
        }
        return $result;
    }
}